{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# The `fxcm_order` Class\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This section introduces the `fxcm_order` class which is created by different operations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import fxcmpy\n",
    "import pandas as pd\n",
    "import datetime as dt\n",
    "con = fxcmpy.fxcmpy(config_file='fxcm.cfg')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The methods `fxcm.create_entry_order()`, `fxcm.create_market_sell_order()`, `fxcm.create_market_buy_order()` and `fxcm.open_trade()` return an instance of the `fxcm_order` class. Existing orders are stored in the attribute `orders`, canceled or executed orders are stored in the attribute `old_orders`, both as instance of the `fxcm_order` class."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Order Ids"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `ids` of existing orders are returned by `con.get_order_ids()`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[96279769, 96277464]\n"
     ]
    }
   ],
   "source": [
    "order_ids = con.get_order_ids()\n",
    "print(order_ids)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The last order id:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "order_id = order_ids[-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`fxcm.orders` is a `dict` object with the order `ids` as keys and the order object as value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{96277464: <fxcm.fxcm_order at 0x7fafab36a780>,\n",
       " 96279769: <fxcm.fxcm_order at 0x7fafab36ad68>}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.orders"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Order Objects"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To get an existing order object, one can use `con.get_order()`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "order = con.get_order(order_id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "accountId:        2815291\n",
      "accountName:      02815291\n",
      "amountK:          400\n",
      "buy:              0\n",
      "currency:         USD/JPY\n",
      "currencyPoint:    35.92664\n",
      "isBuy:            False\n",
      "isELSOrder:       False\n",
      "isEntryOrder:     True\n",
      "isLimitOrder:     False\n",
      "isNetQuantity:    False\n",
      "isStopOrder:      True\n",
      "limit:            0\n",
      "limitPegBaseType: -1\n",
      "limitRate:        0\n",
      "ocoBulkId:        0\n",
      "orderId:          96277464\n",
      "range:            0\n",
      "sell:             110\n",
      "status:           Waiting\n",
      "stop:             111\n",
      "stopMove:         0\n",
      "stopPegBaseType:  -1\n",
      "stopRate:         111\n",
      "time:             2018-01-11 13:41:32\n",
      "timeInForce:      GTC\n",
      "type:             SE\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(order)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get and Set"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `fxcm_order` class has **get methods** for all attributes, for example: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "400"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "order.get_amount()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "order.get_isBuy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "110"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "order.get_sell()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Moreover, the class has **set methods** for the following attributes:\n",
    "\n",
    "* `amount`: `set_amount()`\n",
    "* `limit`: `set_limit_rate()`\n",
    "* `range`: `set_range()`\n",
    "* `rate`: `set_rate()`\n",
    "* `stop`: `set_stop_rate()`\n",
    "* `trailing_step`: `set_trailing_step()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "order.set_amount(600)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "order.set_stop_rate(111, is_in_pips=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>accountId</th>\n",
       "      <td>2815291</td>\n",
       "      <td>2815291</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>accountName</th>\n",
       "      <td>02815291</td>\n",
       "      <td>02815291</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>amountK</th>\n",
       "      <td>500</td>\n",
       "      <td>600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>buy</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>currency</th>\n",
       "      <td>USD/JPY</td>\n",
       "      <td>USD/JPY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>currencyPoint</th>\n",
       "      <td>44.901</td>\n",
       "      <td>53.8813</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>expireDate</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isBuy</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isELSOrder</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isEntryOrder</th>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isLimitOrder</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isNetQuantity</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isStopOrder</th>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>limit</th>\n",
       "      <td>109</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>limitPegBaseType</th>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>limitRate</th>\n",
       "      <td>109</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ocoBulkId</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>orderId</th>\n",
       "      <td>96279769</td>\n",
       "      <td>96277464</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>range</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ratePrecision</th>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sell</th>\n",
       "      <td>110</td>\n",
       "      <td>110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>status</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>stop</th>\n",
       "      <td>0</td>\n",
       "      <td>111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>stopMove</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>stopPegBaseType</th>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>stopRate</th>\n",
       "      <td>0</td>\n",
       "      <td>111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>t</th>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <td>01112018134215</td>\n",
       "      <td>01112018154002315</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>timeInForce</th>\n",
       "      <td>GTC</td>\n",
       "      <td>GTC</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>tradeId</th>\n",
       "      <td>96279770</td>\n",
       "      <td>96277465</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>type</th>\n",
       "      <td>SE</td>\n",
       "      <td>SE</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                               0                  1\n",
       "accountId                2815291            2815291\n",
       "accountName             02815291           02815291\n",
       "amountK                      500                600\n",
       "buy                            0                  0\n",
       "currency                 USD/JPY            USD/JPY\n",
       "currencyPoint             44.901            53.8813\n",
       "expireDate                                         \n",
       "isBuy                      False              False\n",
       "isELSOrder                 False              False\n",
       "isEntryOrder                True               True\n",
       "isLimitOrder               False              False\n",
       "isNetQuantity              False              False\n",
       "isStopOrder                 True               True\n",
       "limit                        109                  0\n",
       "limitPegBaseType              -1                 -1\n",
       "limitRate                    109                  0\n",
       "ocoBulkId                      0                  0\n",
       "orderId                 96279769           96277464\n",
       "range                          0                  0\n",
       "ratePrecision                  3                  3\n",
       "sell                         110                110\n",
       "status                         1                  1\n",
       "stop                           0                111\n",
       "stopMove                       0                  0\n",
       "stopPegBaseType               -1                 -1\n",
       "stopRate                       0                111\n",
       "t                              3                  3\n",
       "time              01112018134215  01112018154002315\n",
       "timeInForce                  GTC                GTC\n",
       "tradeId                 96279770           96277465\n",
       "type                          SE                 SE"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.get_orders().T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Status and Deletion"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A `fxcm_order` object has its own `delete` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Waiting'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "order.get_status()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "order.delete()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Canceled'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "order.get_status()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Canceled Orders"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The **canceled order** can still be found in the `old_orders` attribute."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{96277464: <fxcm.fxcm_order at 0x7fafab36a780>}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.old_orders"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
